import Vue from 'vue'
let self = new Vue();

export default {
    checkRule(path){
        let ruleStr = self.$utils.sessionGet('rule');
        if(ruleStr){
            let ruleArr = JSON.parse(ruleStr);

            let _keys = Object.keys(ruleArr);

            if(_keys.indexOf(path) > -1){
                return true;
            }else{
                return false;
            }
        }

        return false;
    },

    getRoutes(){
        let _rule = JSON.parse(self.$utils.sessionGet('rule'));

        if(_rule) {
            let _routes = [
                {
                    path: '/',
                    name: 'DashBoard',
                    component: () => import('@/views/Home'),
                    redirect: '/dashboard',
                    children:[]
                },
                // {
                //     path: '/home',
                //     name: 'home',
                //     component: () => import('@/views/Home'),
                // }
            ];


            for (let path in _rule) {
                if (path && path !== '#') {
                    let _path = path.split("/");

                    let _pathStr = '';

                    if(_path[_path.length - 1] === 'edit' || _path[_path.length - 1] === 'rule'){
                        _pathStr = path + '/:id';
                    }else{
                        _pathStr = path;
                    }

                    let _route = {
                        path: _pathStr,
                        component: () => import('@/views' + path),
                        meta: [_rule[path]]
                    };
                    _routes[0].children.push(_route);
                    // _routes.push(_route);
                }
            }

            return _routes;
        }else{
            return false;
        }
    }
}
